/*
Copyright 2010 Farent-Solutions Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 */
package com.farent.solutions.plugins.aribaweb;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Action;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeListener;
import org.netbeans.spi.project.ui.support.NodeFactory;
import org.netbeans.spi.project.ui.support.NodeList;
import org.openide.nodes.Node;
import org.openide.util.ChangeSupport;
import org.netbeans.api.project.Project;
import org.netbeans.modules.java.api.common.ant.UpdateHelper;
import org.netbeans.modules.java.api.common.classpath.ClassPathSupport;
import org.netbeans.modules.java.api.common.project.ProjectProperties;
import org.netbeans.modules.java.api.common.project.ui.LibrariesNode;
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
import org.netbeans.spi.project.support.ant.ReferenceHelper;

/**
 *
 * @author farouka
 */
@NodeFactory.Registration(projectType = "com-farent-solutions-plugins-aribaweb-AWNetbeansProject", position = 400)
public final class AWNetbeansLibrariesNodeFactory implements NodeFactory {

    public AWNetbeansLibrariesNodeFactory() {
    }

    @Override
    public NodeList createNodes(Project p) {
        AWNetbeansProject project = p.getLookup().lookup(AWNetbeansProject.class);
        assert project != null;
        return new LibrariesNodeList(project);
    }

    private static class LibrariesNodeList implements NodeList<String>, PropertyChangeListener {

        private static final String LIBRARIES = "libs"; //NOI18N
        private AWNetbeansProject project;
        private final ChangeSupport changeSupport = new ChangeSupport(this);
//
        private PropertyEvaluator evaluator;
        private UpdateHelper helper;
        private ReferenceHelper refHelper;
        private final ClassPathSupport cs;
//        private final SourceRoots testSources;

        /** Creates a new instance of LibrariesNodeFactory */
        public LibrariesNodeList(AWNetbeansProject proj) {
            project = proj;
//            testSources = project.getTestSourceRoots();
            AWNetbeansLogicalView logView = project.getLookup().lookup(AWNetbeansLogicalView.class);
            assert logView != null;
            evaluator = logView.getEvaluator();
            helper = logView.getUpdateHelper();
            refHelper = logView.getRefHelper();
            cs = new ClassPathSupport(evaluator, refHelper, helper.getAntProjectHelper(), helper, null);
        }

        @Override
        public List<String> keys() {
            List<String> result = new ArrayList<String>();
            result.add(LIBRARIES);
            return result;
        }

        @Override
        public void addChangeListener(ChangeListener l) {
            changeSupport.addChangeListener(l);
        }

        @Override
        public void removeChangeListener(ChangeListener l) {
            changeSupport.removeChangeListener(l);
        }

        @Override
        public Node node(String key) {
            if (key.equalsIgnoreCase(LIBRARIES)) {
                //Libraries Node
                return new LibrariesNode("Libraries",
                        project, evaluator, helper, refHelper,"javac.classpath" /**ProjectProperties.RUN_CLASSPATH**/,
                        new String[]{ProjectProperties.BUILD_CLASSES_DIR},
                        "platform.active", // NOI18N
                        new Action[]{
                            LibrariesNode.createAddLibraryAction(refHelper, project.getSourceRoots(), null),
                            LibrariesNode.createAddFolderAction(project.getAntProjectHelper(), project.getSourceRoots()),
                            null,
//                            ProjectUISupport.createPreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE), // NOI18N
                        },
                        null,
                        cs,
                        null);
            }
            assert false : "No node for key: " + key;
            return null;
        }

        @Override
        public void addNotify() {
        }

        @Override
        public void removeNotify() {
        }

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            // The caller holds ProjectManager.mutex() read lock
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    changeSupport.fireChange();
                }
            });
        }
    }
}
